/**
 * 由邀请好友凑步数加载进入的入口
 */
import CommonPage from "../../common/CommonPage.js"
const app = getApp()
const api = require('../../api/http.js')
const config = require('../../../config.js')
const util = require('../../../utils/util.js')

class InvitationPage extends CommonPage {

  /**
   * 页面的初始数据
   */
  constructor(...args) {
    super(...args)
    super.$init({
      myName: '',
      friendName: '',
      myStepCount: 0,
      friendStepCount: 0,
      myStep: 0,
      friendOpenId: null,
      friendStep: 0,
      inStep: 0,
      height: '128rpx',
      nannerTitleColorLeft: "#4D7BFE",
      nannerTitleColorRight: "#000",
      bannerIndex: 0,
      showImage: true,
      titleName: '赠送活力值',
      backImagePath: app.globalData.backImagePath
    })
  }
  initData = function(query, resolve, reject) {
    console.log("预初始化邀请界面：", query)
    if (query.friendOpenId) {
      this.data.myName = query.myName
      this.data.myOpenId = query.myOpenId
      this.data.myStep = query.myStep
      this.data.friendName = query.friendName
      this.data.friendStep = query.friendStep
      this.data.friendOpenId = query.friendOpenId
      this.$setData(this.data)
    }
  }

  $onNavigator(query) {
    console.log("邀请好友凑步数；参数：", query)
    this.$put("invitation-data", this.initData.bind(this), query)
  }
  back = function(params) {
    // console.log("你点击了返回");
    wx.navigateBack();
  }
  
  getStep = function() {
    
  }
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad = function (args) {
    console.log("Invitation arg onLoad:", args)
    var that = this
    var dat = this.$take("invitation-data")
    if (dat) return
    this.initData(args)
  }

  /**
   * 生命周期函数--监听页面显示
   */
  onShow = function () {
    // console.log("Data:", this.data)
  }


  confirmDonate = function(args) {
    console.log("args", args)
    var from = app.globalData.userInfo.openId
    var to = args.currentTarget.dataset.openid
    if (from == to) {  
      wx.switchTab({
        url: '../index',
        complete: () => {
          wx.showToast({
            title: '不可赠送给自己！',
            icon: 'none',
            image: '',
            duration: 1000,
            mask: true,
          })
        }
      })
    } else{
      if (args.currentTarget.dataset.step < 1) {
        console.log("小于1")
        wx.switchTab({
          url: '../index',
        })
      } else {
        console.log("大于0")
        var param = {
          fromOpenId: from,
          toOpenId: to,
          step: args.currentTarget.dataset.step
        }
        console.log("param", param)
        api.post(config.donateStepUrl, param).then(res => {
          console.log("res", res)
          wx.switchTab({
            url: '../index',
          })
        })
      }     
    }    
  }
  inLifeVal = function (e) {
    var step = Number(e.detail.value)
    if (isNaN(step)) {
      wx.showToast({
        title: '请输入数字',
        icon: 'none'
      })
    } else {
      if (step > app.globalData.step || step < 0) {
        wx.showToast({
          title: '请输入0到' + app.globalData.step + '的值',
          icon: 'none'
        })
      } else {
        this.setData({
          inStep: step,
        })
      }
    }
  }
  nannerTitleColorLeftClick = function(params) {
    var that=this;
    that.setData({
      nannerTitleColorLeft:'#4D7BFE',
      nannerTitleColorRight:'#000',
      bannerIndex:0,
    })
  }
  nannerTitleColorRightClick = function(params) {
    var that=this;
    that.setData({
      nannerTitleColorLeft:'#000',
      nannerTitleColorRight:'#4D7BFE',
      bannerIndex:1,
    })
  }
  bannerChange = function(event) {
    var that=this;
    var index=event.detail.current;
    if(index===0)
    that.setData({
      nannerTitleColorLeft:'#4D7BFE',
      nannerTitleColorRight:'#000',
    });
    else that.setData({
      nannerTitleColorLeft:'#000',
      nannerTitleColorRight:'#4D7BFE',
    });
  }
}

Page(new InvitationPage({clazzName: "InvitationPage"}))